package com.apptonelabs.android.controller;

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.speech.tts.TextToSpeech;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;

import com.apptonelabs.android.R;
import com.apptonelabs.android.model.PNRStatus;
import com.apptonelabs.android.model.PassengerDetails;

/**
 * @author varma.ponnamanda 1154177041
 */
public class PNRStatusActivity extends Activity implements OnClickListener,
		TextToSpeech.OnInitListener {
	/** Called when the activity is first created. */
	private String pnr = "Un Known";

	private TextView pnrView;
	private TextView journeyDate;
	private TextView trainNumber;
	private TextView trainName;
	private TextView trainTo;
	private TextView trainFrom;
	private TextView reservType;
	private TextView boardingPoint;
	private TextView reservUpTo;
	private TextView passengerData;

	private TextToSpeech tts;
	private PNRStatus pnrStatus = null;

	private Button speakOut;
	private Button back;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
		// No title bar
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);

		setContentView(R.layout.pnrdetails);

		// TTS
		tts = new TextToSpeech(this, this);
		tts.setLanguage(Locale.getDefault());
		tts.setPitch(0.6F);
		tts.setSpeechRate(0.5F);

		pnr = getIntent().getExtras().getString("pnr");
		pnrStatus = (PNRStatus) getIntent().getExtras().getSerializable(
				"pnrStatus");

		back = (Button) findViewById(R.id.backButton);
		pnrView = (TextView) findViewById(R.id.pnrnumber);
		journeyDate = (TextView) findViewById(R.id.journeyDate);
		trainNumber = (TextView) findViewById(R.id.trainNumber);
		trainName = (TextView) findViewById(R.id.trainName);
		trainTo = (TextView) findViewById(R.id.trainTo);
		trainFrom = (TextView) findViewById(R.id.trainFrom);
		reservType = (TextView) findViewById(R.id.reservType);
		boardingPoint = (TextView) findViewById(R.id.boardingPoint);
		reservUpTo = (TextView) findViewById(R.id.reservUpTo);
		passengerData = (TextView) findViewById(R.id.passengerData);

		speakOut = (Button) findViewById(R.id.speakBT);

		pnrView.setText("PNR :" + pnr);
		journeyDate.setText("Journey Date :"
				+ pnrStatus.getTrainDetails().getBoardingDate());
		trainNumber.setText("Train Number :"
				+ pnrStatus.getTrainDetails().getNumber());
		trainName.setText("Train name :"
				+ pnrStatus.getTrainDetails().getName());
		trainTo.setText("Train To   :" + pnrStatus.getTrainDetails().getTo());
		trainFrom.setText("Resevation From :"
				+ pnrStatus.getTrainDetails().getFrom());
		boardingPoint.setText("Baording Poit :"
				+ pnrStatus.getTrainDetails().getBoardingPoint());
		reservUpTo.setText("Reservation Upto :"
				+ pnrStatus.getTrainDetails().getReservedUpto());
		reservType.setText("Class :"
				+ pnrStatus.getTrainDetails().getReservedType() + ", "
				+ pnrStatus.getTrainDetails().getChatStatus());

		StringBuffer buff = new StringBuffer();
		buff.append("Name         BookedStaus CurrentStaus\n");

		for (int i = 0; i < pnrStatus.getPassengerDetails().size(); i++) {
			PassengerDetails pd = pnrStatus.getPassengerDetails().get(i);
			buff.append(pd.getName() + "   " + pd.getBookingStatus() + "   "
					+ pd.getCurrentStatus() + "\n");
		}

		passengerData.setText(buff.toString());

		back.setOnClickListener(this);
		speakOut.setOnClickListener(this);
	}

	public void onClick(View src) {
		switch (src.getId()) {
		case R.id.backButton:
			this.finish();
			break;
		case R.id.speakBT:
			speakOut();
			break;
		}
	}

	public boolean isNetworkAvailable() {
		ConnectivityManager cm = (ConnectivityManager) getSystemService(Context.CONNECTIVITY_SERVICE);
		NetworkInfo networkInfo = cm.getActiveNetworkInfo();
		// if no network is available networkInfo will be null, otherwise check
		// if we are connected
		if (networkInfo != null && networkInfo.isConnected()) {
			return true;
		}
		return false;
	}

	@Override
	public void onInit(int status) {
		if (status == TextToSpeech.SUCCESS) {
			int result = tts.setLanguage(Locale.US);
			if (result == TextToSpeech.LANG_MISSING_DATA
					|| result == TextToSpeech.LANG_NOT_SUPPORTED) {
				speakOut.setEnabled(false);
			} else {
				speakOut.setEnabled(true);
			}
		} else {
			speakOut.setEnabled(false);
		}
	}

	@Override
	public void onDestroy() {
		// Don't forget to shutdown tts!
		if (tts != null) {
			tts.stop();
			tts.shutdown();
		}
		super.onDestroy();
	}

	private void speakOut() {
		String text = "Current Status of the PNR Number. ";
		for (int i = 0; i < pnrStatus.getPassengerDetails().size(); i++) {
			PassengerDetails pd = pnrStatus.getPassengerDetails().get(i);
			text += ("For Passenger number " + (i+1) + " " + pd.getCurrentStatus() + ". ");
		}
		
		if(pnrStatus.getTrainDetails().getChatStatus().contains("NOT")) {
			text += ("Please note that Chart has not prepared, check the status after some time.");
		} else {
			text += ("Please note that Chart has been prepared");
		}
		
		tts.speak(text, TextToSpeech.QUEUE_FLUSH, null);
	}
}